package com.bethinner.looterredbag;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.app.Notification;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

import util.Notifier;
import util.RedEnvelopeHelper;
import util.XLog;

public class LooterService extends AccessibilityService {

    private static final String TAG = "LooterService";
    //整个窗口的快照
    private AccessibilityNodeInfo accessibilityNodeInfo = null;

    private PowerManager.WakeLock wakeLock;
    /**
     * {@inheritDoc}
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onServiceConnected() {
        Notifier.getInstance().notify(getString(R.string.app_name), getString(R.string.wechat_acc_service_start_notification), getString(R.string.wechat_acc_service_start_notification),
                Notifier.TYPE_WECHAT_SERVICE_RUNNING, false);

        PowerManager manager = ((PowerManager)getSystemService(POWER_SERVICE));
         wakeLock = manager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, "ATAAW");
        wakeLock.acquire();
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        handleNotificationChange(event);


        accessibilityNodeInfo = event.getSource();
        if (null==accessibilityNodeInfo){
            return;
        }
        //如果当前的事件类型是当前窗口内同发生了变化，那么判断是否有红包视图出现
        if (event.getEventType()== AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED){
            List<AccessibilityNodeInfo> hongbaoList =accessibilityNodeInfo.findAccessibilityNodeInfosByText("领取红包");
            if (hongbaoList.size()>0){
                AccessibilityNodeInfo curNodeInfo = hongbaoList.get(hongbaoList.size()-1);
                curNodeInfo.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }

        if (event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
            List<AccessibilityNodeInfo> clickWinodwList = accessibilityNodeInfo.findAccessibilityNodeInfosByText("拆红包");
            if (clickWinodwList.size()>0){
                AccessibilityNodeInfo curNodeInfo = clickWinodwList.get(clickWinodwList.size()-1);
                curNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
    }

    @Override
    public void onInterrupt() {

    }
    public static void log(String message) {
        XLog.e("WechatAccService", message);
    }
    public void handleNotificationChange(AccessibilityEvent event) {
        log("eventtype:" + event.getEventType());
        if (event == null)
            return;

        if (!(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)) {
            return;
        }
        if (event.getParcelableData() instanceof Notification) {
            Notification notification = (Notification) event
                    .getParcelableData();
            if (notification.tickerText != null
                    && notification.tickerText.toString().contains(getString(R.string.wechat_acc_service_red_envelope_notification_identification))) {
                log("来红包啦 get red envelope message");
                RedEnvelopeHelper.openNotification(event);
            }
        }
    }


    public void onDestroy() {
        Notifier.getInstance().cancelByType(
                Notifier.TYPE_WECHAT_SERVICE_RUNNING);
        wakeLock.release();
        super.onDestroy();
    }
}
